// pages/function/exam_list/exam_list.js
const db = wx.cloud.database({});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exam: [],
    user_vip: '',
    user_default:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let openId = wx.getStorageSync('openId')
    db.collection("user").doc(openId).get()
      .then(res => {
        this.setData({
          user_vip: res.data.user_vip,
          user_default:res.data.user_default
        })
        wx.cloud.callFunction({
            // 云函数名称
            name: 'getexam_list',
            data: {
              subject: Number(res.data.user_default),
            }
          })
          .then(res => {
            console.log('全部获取', res)
            this.setData({
              exam: res.result.data
            })
          })
          .catch(err => {
            console.log('全部获取失败', err)
          })
      })
  },
  //考试记录点击跳转
  exam_log() {
    let user_default = this.data.user_default
    wx.redirectTo({
      url: '/pages/function/exam_log/exam_log?user_default='+user_default,
    })
    
  },
  //用户点击试题函数，跳转去做题
  come_exam(e) {
    // 1.判断用户有没有做题资格
    let vip = this.data.user_vip
    let user_default = this.data.user_default
    // console.log(vip)
    // console.log(e.currentTarget.id)
    let arr = e.currentTarget.id.split('+')
    // console.log(arr[1])
    for (let index = 0; index < vip.length; index++) {
      if (user_default == vip[index].subject ) {
        // console.log('tg ')
    //当会员值小于或等于都跳转
        if (vip[index].type<=arr[1]) {
          wx.navigateTo({
            url: '/pages/function/exam_index/exam_index?exam_id=' + arr[0]+'&exam_name='+arr[2],
          })
    //否则跳转去开vip
        }else {
          wx.navigateTo({
            url: '/pages/vip/vip_index/vip_index',
          })
        }
      }
      }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})